package com.boxin.ims.modules.wechat.busi;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.boxin.ims.modules.momarketing.common.ProjectUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.wechat.entity.ImageMessage;
import com.boxin.ims.modules.wechat.entity.LocationMessage;
import com.boxin.ims.modules.wechat.entity.NewsReply;
import com.boxin.ims.modules.wechat.entity.TextMessage;
import com.boxin.ims.modules.wechat.entity.UserDefine;
import com.boxin.ims.modules.wechat.entity.WeChat;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.WechatMemberService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;


/**
 * @author Jakemanse
 * @date 2013-9-23
 * @function <p> 微信业务数据处理 基类</p>
 */
public abstract class BusinessProcessor {
	
	protected final Logger logger = Logger.getLogger(this.getClass());
	
	
	protected String receiveMsg;	//用户发的消息信息
	protected String sendMsg;		//给 用户发送的数据信息
	protected User user;			//所属店家
	protected WeChat weChat;		//平台配置对象
	protected String openId;		//微信用户开放Open ID
	protected boolean isNeedMember;	//是否需要是会员
	protected HttpServletRequest request;	//页面请求对象
	protected HttpServletResponse response;	//写页面对象
	
	protected Map<String,String> messageMap ;
	protected TextMessage textMessage;			//文本消息包装对象
	protected LocationMessage locationMessage;	//位置消息
	
	@Autowired
	WechatMemberService wechatMemberService;
	
	
	public BusinessProcessor(){
		
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-11  下午10:28:25
	 * @function <p> 检查该会员是否已经绑定，如果没有绑定直接回复 </p>
	 * @return
	 */
	public WechatMember  memberCheckAndReply(){
		
		WechatMember member =	wechatMemberService.getByWechatMemberId(user, openId);
		if(member == null){
			//String reply = "亲，您还不是我们的会员请<a href=\"http://www.hdzhx.com/zxims/f/wechat/bind/"+user.getId() +"?wechatId="+openId+"&topage=scoreexchange\">点我申请微会员</a>谢谢！";
			//writeToUser(reply);
			
			String title = "微会员申请";
			UserDefine userDefine = WechatUtils.getUserDefineByUserId(weChat.getUser().getId());
			if(userDefine != null && StringUtils.isNotEmpty(userDefine.getRegMemberTitle())){
				title = userDefine.getRegMemberTitle();
			}
			
			String desc = "亲,您还不是我们的微会员,点击图片进入微会员申请页面赶紧注册成微会员吧。成为我们的会员后，您将可以参与各种优惠活动以及更好的服务,谢谢!";
			if(userDefine != null && StringUtils.isNotEmpty(userDefine.getRegMemberDesc())){
				desc = userDefine.getRegMemberDesc();
			}
			String picName = "reg.jpg";
			if(userDefine!=null && StringUtils.isNotEmpty(userDefine.getRegMemberPic())){
				picName = userDefine.getRegMemberPic();
			}
			String picUrl = "http://www.hdzhx.com/zxims/userfiles/images/"+picName;
			picUrl ="http://www.hdzhx.com/zxims/userfiles/images/"+picName+"?t="+System.currentTimeMillis();
			
			String url =  "http://www.hdzhx.com/zxims/f/wechat/bind/"+user.getId() +"?wechatId="+openId;
			writeLinkImageToUser(title, desc, picUrl, url);
			
		}
		return member;
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-11  下午11:20:43
	 * @function <p>  </p>
	 * @return
	 */
	public WechatMember getMember(){
		return wechatMemberService.getByWechatMemberId(user, openId);
	}
	
	public BusinessProcessor(WeChat weChat,String openId,HttpServletRequest request,HttpServletResponse response){
		this.weChat = weChat;
		this.openId = openId;
		this.request = request;
		this.response = response;
		this.user = weChat.getUser();
	}
	
	public BusinessProcessor(WeChat weChat,String openId){
		this.weChat = weChat;
		this.openId = openId;
				
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-7  上午12:45:53
	 * @function <p> 向中奖用户发送电子凭证 </p>
	 * @param title
	 * @param desc
	 * @param qrcodeId
	 */
	public void writeImageToUser(String title,String desc,Long qrcodeId){
		ImageMessage imageMessage = new ImageMessage(messageMap);
		imageMessage.setArticleCount("1");
		imageMessage.setTitle(title);
		imageMessage.setDescription(desc+ getQrcodeMessage());
		imageMessage.setPicUrl(QRCodeUtils.getQRCodePNGURL(qrcodeId));
		imageMessage.setUrl(QRCodeUtils.getQRCodePNGPageURL(qrcodeId)+"&uid="+getUser().getId());
		try {
			String reply = imageMessage.replyMessage();
			response.getWriter().print(reply );
			logger.info(reply);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-12  下午1:12:26
	 * @function <p> 查询奖品回复内容 </p>
	 * @param title
	 * @param desc
	 * @param qrcodeId
	 */
	public void writeSingleLotteryImageToUser(String title,String desc,Long qrcodeId){
		ImageMessage imageMessage = new ImageMessage(messageMap);
		imageMessage.setArticleCount("1");
		imageMessage.setTitle(title);
		imageMessage.setDescription(desc+ getQrcodeMessage());
		imageMessage.setPicUrl(QRCodeUtils.getQRCodePNGURL(qrcodeId));
		imageMessage.setUrl(QRCodeUtils.getQRCodePNGPageURL(qrcodeId));
		try {
			String reply = imageMessage.replyMessage();
			response.getWriter().print(reply );
			logger.info(reply);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @author Jakemanse
	 * @time 2014-1-28  下午10:37:07
	 * @function <p>全局通知信息  </p>
	 * @return
	 */
	private String getQrcodeMessage(){
		
		UserDefine userDefine =	WechatUtils.getUserDefineByUserId(user.getId());
		if(userDefine != null && com.thinkgem.jeesite.common.utils.StringUtils.isNotEmpty(userDefine.getQrcodeMessage())){
			return userDefine.getQrcodeMessage();
		}
		return "";
	}
	
	
	
	
	public void writeQueryLotteryImageToUser( List<NewsReply> newsReplyList){
		ImageMessage imageMessage = new ImageMessage(messageMap);
		try {
			String reply = imageMessage.replyMessage(newsReplyList);
			response.getWriter().print(reply );
			logger.info(reply);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-1-16  下午4:50:59
	 * @function <p> 多图文回复 </p>
	 * @param newsReplyList
	 */
	public void writeMultiImagesToUser( List<NewsReply> newsReplyList){
		ImageMessage imageMessage = new ImageMessage(messageMap);
		try {
			String reply = imageMessage.replyMessage(newsReplyList);
			response.getWriter().print(reply );
			logger.info(reply);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	
	public void writeImageXMLToUser(String xml){
		try {
			response.getWriter().print(xml );
			logger.info(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	/**
	 * @author Jakemanse
	 * @time 2013-10-7  上午12:49:55
	 * @function <p> 导航连接图片 </p>
	 * @param title	图片标题
	 * @param desc  图片描述
	 * @param picUrl	图片地址
	 * @param url		图片连接地址
	 */
	public void writeLinkImageToUser(String title,String desc,String picUrl,String url){
		ImageMessage imageMessage = new ImageMessage(messageMap);
		imageMessage.setArticleCount("1");
		imageMessage.setTitle(title);
		imageMessage.setDescription(desc );
		imageMessage.setPicUrl(picUrl);
		imageMessage.setUrl(url);
		try {
			String reply = imageMessage.replyMessage();
			response.getWriter().print(reply );
			logger.info(reply);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-2  上午9:27:36
	 * @function <p>回复用户信息  </p>
	 * @param reply
	 */
	public void writeToUser(String reply){
		//返回给微信用户的信息
		try {
			TextMessage txtMsg = new TextMessage(messageMap);
			txtMsg.setContent(reply +  getQrcodeMessage());
			response.getWriter().print(txtMsg.replyMessage() );
			logger.info(txtMsg.replyMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-3  上午9:07:15
	 * @function <p> 未到所有关键词的回复，则回配置的默认内容 </p>
	 */
	public void writeDefaultToUser(){
		writeToUser(weChat.getRemarks()) ;
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-23  下午11:04:29
	 * @function <p> 消息处理后的返回结果 </p>
	 * @return
	 */
	public abstract ProcessMessage process();
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-2  下午3:24:57
	 * @function <p> 是否必须要求是会员 </p>
	 * @return
	 */
	public abstract boolean isNeedBeMember();
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-3  上午8:43:01
	 * @function <p> 写调试信息  </p>
	 * @param reply
	 */
	public void printReplyLogger(String reply){
		logger.debug("回复的全内容是:"+reply);
	}
	
	
	
	
	

	public LocationMessage getLocationMessage() {
		return locationMessage;
	}

	public void setLocationMessage(LocationMessage locationMessage) {
		this.locationMessage = locationMessage;
	}

	public String getReceiveMsg() {
		return receiveMsg;
	}

	public void setReceiveMsg(String receiveMsg) {
		this.receiveMsg = receiveMsg;
	}

	public String getSendMsg() {
		return sendMsg;
	}

	public void setSendMsg(String sendMsg) {
		this.sendMsg = sendMsg;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	public WeChat getWeChat() {
		return weChat;
	}

	public void setWeChat(WeChat weChat) {
		this.weChat = weChat;
	}

	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public Map<String, String> getMessageMap() {
		return messageMap;
	}

	public void setMessageMap(Map<String, String> messageMap) {
		this.messageMap = messageMap;
	}

	public TextMessage getTextMessage() {
		return textMessage;
	}

	public void setTextMessage(TextMessage textMessage) {
		this.textMessage = textMessage;
	}

	public Logger getLogger() {
		return logger;
	}
	
	public void setNeedMember(boolean isNeedMember){
		this.isNeedMember = isNeedMember;
	}
	
	
	
	

}
